package com.hunmengwl.oauth.common.util;


import com.hunmengwl.oauth.common.enums.StatusEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author hunmeng
 * @create 2020-01-03 10:44
 */
@Getter
@Setter
public class Result implements Serializable {

    /**
     * 结果标记(true:执行成功 false:执行失败)
     */
    private Boolean flag;
    /**
     * 消息状态码
     */
    private Integer code;
    /**
     *  消息
     */
    private String msg;
    /**
     * 返回数据
     */
    private Object data;
    /**
     * 分页用于的统计
     */
    private Integer total;

    private Result(Boolean flag, Integer code, String msg, Object data) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Result(Boolean flag, Integer code, String msg, Object data, Integer total) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.total = total;
    }


    public static Result success(StatusEnum statusEnum, Object data, Integer total) {
        return new Result(true, statusEnum.Code, statusEnum.msg, data, total);
    }

    /**
     * 响应成功(带返回数据)
     *
     * @param data 返回数据
     * @return Result
     */
    public static Result success(Object data) {
        return new Result(true, StatusEnum.SUCCESS.Code, "响应成功", data);
    }

    /**
     * 响应成功
     *
     * @return Result
     */
    public static Result success() {
        return new Result(true, StatusEnum.SUCCESS.Code, StatusEnum.SUCCESS.msg, null);
    }

    /**
     * 响应成功，自定义消息
     *
     * @param msg
     * @return
     */
    public static Result success(String msg) {
        return new Result(true, StatusEnum.SUCCESS.Code, msg, null);
    }

    /**
     * 响应成功——自定义消息和内容
     *
     * @param msg
     * @param data
     * @return
     */
    public static Result success(String msg, Object data) {
        return new Result(true, StatusEnum.SUCCESS.Code, msg, data);
    }

    public static Result success(StatusEnum statusEnum) {
        return new Result(true, statusEnum.Code, statusEnum.msg, null);
    }

    public static Result success(StatusEnum statusEnum, Object data) {
        return new Result(true, statusEnum.Code, statusEnum.msg, data);
    }

    /**
     * 响应成功-自定义所有内容
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Result success(int code, String msg, Object data) {
        return new Result(true, code, msg, data);
    }

    /**
     * 响应错误(不带状态码,带消息)
     *
     * @return Result
     */
    public static Result error(String msg) {
        return new Result(false, StatusEnum.FAIL.Code, msg, null);
    }

    /**
     * 返回错误
     *
     * @param msg
     * @return
     */
    public static Result error(StatusEnum msg) {
        return new Result(false, msg.Code, msg.msg, null);
    }

    /**
     * 响应错误(带错误码,消息提醒)
     *
     * @return
     */
    public static Result errorMsg(Integer code, String msg) {
        return new Result(false, code, msg, null);
    }

}
